'use strict';
import { Controller } from 'egg';

export default class RoleController extends Controller {
    public async list() {
        const {ctx} = this;
        const roleList = await ctx.service.role.getList()
        ctx.body = {
            error_code: ctx.helper.addOne(0),
            msg: 'get success',
            result: roleList
        }
    }

    async save() {
        const {ctx} = this;
        const param = ctx.request.body;
        const roleData = param.roleData;
        let hasSave = false;
        let msg: string='';
        if (roleData._id) {
            await ctx.service.role.update(roleData).then((res:boolean) => {
                console.log(res);
                hasSave = res
                msg='更新成功'
            }).catch(()=>{
                msg='更新失败';
            })
        } else {
            await ctx.service.role.save(roleData).then((res:boolean) => {
                hasSave = res;
                msg = '保存成功';
            }).catch(e => {
                console.log(e);
                msg = '保存失败';

            })
        }
        ctx.body = {
            success: hasSave,
            msg:msg
        }
    }

    async delete() {
        const {ctx} = this;
        const param = ctx.request.body;
        const id = param.id;
        let hasDelete = false;
        if (id) {
            await ctx.service.role.delete(id).then((res:any) => {

                hasDelete = res.deletedCount > 0 ? true : false;
            }).catch(e=>{
                console.log(e);
            })
        }
        ctx.body={
            success: hasDelete
        }
    }
}
export interface resType {
    deletedCount:number
}

